package com.itheima.demo;

import java.util.ArrayList;

public class TypeDemo {//泛型方法练习
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        //System.out.println(getE(arrayList,1));
        arrayList.add("100");
        arrayList.add(true);
        arrayList.add(101);
        arrayList.add('A');
        System.out.println(getE(arrayList,0));
        System.out.println(getE(arrayList,1));
        System.out.println(getE(arrayList,2));
        System.out.println(getE(arrayList,3));
        //System.out.println(getE(arrayList,4));
    }
    //根据索引和集合获得对应元素
    public static <E>E getE (ArrayList<E> arrayList,int index){
        if(arrayList == null){//非法论证错误
            throw new IllegalArgumentException("集合为空");
        }
        if(index<0 || index>arrayList.size()){
            throw  new IndexOutOfBoundsException("索引超界");
        }
        return arrayList.get(index);
    }
}
